package com.itany.corejava.code09_集合.map集合;

import java.util.*;

/**
 * @author 石小俊
 * @date 2023年05月08日 14:39
 */
public class Test04_Properties {

    public static void main(String[] args) {
        // 定义一个Properties集合
        Properties properties = new Properties();

        // 由于Properties是继承自Hashtable,因此可以使用Hashtable的方法
        // 但是开发中一般不会这么用
        // properties.put("id",1);
        // properties.put("name","admin");
        // properties.put("age",19);


        // 使用Properties时,一般使用Properties自己的方法
        // setProperty(String key,String value):添加数据
        properties.setProperty("id", "1");
        properties.setProperty("username", "admin");
        properties.setProperty("password", "123456");

        // getProperty(key):取值
        System.out.println(properties.getProperty("username"));
        System.out.println(properties);

        // 遍历
        // 方式一:keySet()
        Set<Object> keys = properties.keySet();
        for (Object key : keys) {
            String value = properties.getProperty((String) key);
            System.out.println("key:" + key + ",value:" + value);
        }

        // 方式二:values()
        Collection<Object> values = properties.values();
        for (Object value : values) {
            System.out.println(value);
        }

        // 方式三:entrySet()
        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        Iterator<Map.Entry<Object, Object>> it = entries.iterator();
        while (it.hasNext()) {
            Map.Entry<Object, Object> entry = it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key:" + key + ",value:" + value);
        }

        // 方式四:Enumeration遍历key
        Enumeration<Object> elements = properties.keys();
        while (elements.hasMoreElements()) {
            String key = (String) elements.nextElement();
            String value = properties.getProperty(key);
            System.out.println("key:" + key + ",value:" + value);
        }

        // 方式五:Enumeration遍历value
        Enumeration<Object> elements2 = properties.elements();
        while (elements2.hasMoreElements()) {
            Object value = elements2.nextElement();
            System.out.println(value);
        }
    }

}
